package com.pronhub.cloud.shop.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

public class MyRedisTemplate {
    //就是设计几个方法来操作Redis就是吧自己打成一个Jar包给别人用
    @Autowired
    private JedisPool JedisPool;
    //set方法的设计
    /**
     *
     * @param keyPrefix RedisKye的前缀名字
     * @param key 钥匙
     * @param data 数据
     * @param <T> 泛型
     */
    public <T> void set(KeyPrefix keyPrefix,String key,T data){
        //主要目的是操作Redis,往里面存入数据
        //JDK1.7之后有的特性,会在用完资源后自动释放资源
        try (Jedis jedis = JedisPool.getResource();){
            //得到realKey
            String realKey = keyPrefix.getPrefix()+key;
            //是否有失效时间
            if (keyPrefix.getExpireSecond()>0){
                //有失效时间
                jedis.setex(realKey,keyPrefix.getExpireSecond(), JSON.toJSONString(data));
            }else {
                //没有失效时间 长久保存
                jedis.set(realKey,JSON.toJSONString(data));
            }
            //如果有异常 抛出
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    //get方法
    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){
        //根据开得到东西
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            String objStr = jedis.get(realKey);
            return JSON.parseObject(objStr,clazz);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    //exists是否存在Redis中
    public boolean exists(KeyPrefix keyPrefix,String key){
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            Boolean exists = jedis.exists(realKey);
            return exists;
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    //expire方法 给Key设置失效时间 重新设置
    public Long expire(KeyPrefix keyPrefix,String key,int expireSecend){
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            return jedis.expire(realKey,expireSecend);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    //incr方法 数值的递增
    public Long incr(KeyPrefix keyPrefix,String key){
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            return jedis.incr(realKey);//对某一个数值进行自增 类似i++
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    //decr方法 自动相减
    public Long decr(KeyPrefix keyPrefix,String key){
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            return jedis.decr(realKey);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    //设计几个方法 在商品秒杀预热的时候进行的
    //hset 往Redis里面存入数据Hash类型数据
    public <T> void hset(KeyPrefix keyPrefix,String key,String field,T data){
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            jedis.hset(realKey,field,JSON.toJSONString(data));
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    //hget
    public <T> T hget(KeyPrefix keyPrefix,String key,String field,Class<T> clazz){
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            String str = jedis.hget(realKey, field);
            if (str==null){
                return null;
            }
            return JSON.parseObject(str,clazz);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    //hgetAll
    public <T> Map<String,T> hgetAll(KeyPrefix keyPrefix, String key, Class<T> clazz){
        try (Jedis jedis = JedisPool.getResource();){
            String realKey = keyPrefix.getPrefix()+key;
            //得到数据
            Map<String, String> map = jedis.hgetAll(realKey);
            if (map==null){
                return null;
            }
            Map<String,T> resultMap = new HashMap<>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                resultMap.put(entry.getKey(),JSON.parseObject(entry.getValue(),clazz));
            }
            return resultMap;
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
}
